package com.Java.train.business.controller;

import com.Java.train.business.req.ConfirmOrderDoReq;
import com.Java.train.business.service.ConfirmOrderService;
import com.Java.train.business.service.Impl.BeforeConfirmOrderService;
import com.Java.train.common.exception.BusniessExceptionEnum;
import com.Java.train.common.response.CommonResp;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* @author luomoxingchen
* @since 2024-10-04
*/
@RestController
@RequestMapping("/confirm-order")
public class ConfirmOrderController {
    private static final Logger LOG = LoggerFactory.getLogger(ConfirmOrderController.class);

    @Autowired
    private ConfirmOrderService confirmOrderService;
    @Resource
    private BeforeConfirmOrderService beforeConfirmOrderService;

    @PostMapping("/do")
    @SentinelResource(value = "doConfirm", blockHandler = "doConfirmBlock")
    public CommonResp<Object> doConfim(@Valid @RequestBody ConfirmOrderDoReq confirmOrderDoReq) {
//        confirmOrderService.doConfim(confirmOrderDoReq);
        confirmOrderDoReq.setImageCode("123");
        confirmOrderDoReq.setImageCodeToken("123");
        Long id = beforeConfirmOrderService.beforeDoConfirm(confirmOrderDoReq);
        return new CommonResp<>(String.valueOf(id));
    }

    @GetMapping("/query-line-count/{id}")
    public CommonResp<Integer> queryOrderCount(@PathVariable Long id){
        Integer count = confirmOrderService.queryOrderCount(id);
        return new CommonResp<>(count);
    }

    @GetMapping("/cancel/{id}")
    public CommonResp<Object> cannelOrder(@PathVariable Long id){
        Long count = confirmOrderService.cancelOrder(id);
        return new CommonResp<>(count);
    }
    private CommonResp<Object> doConfirmBlock(ConfirmOrderDoReq req, BlockException e){
        LOG.info("ConfirmOrderController购票被限流：{}", req);
//        throw new BusniessException(BusniessExceptionEnum.CONFIRM_ORDER_FLOW_EXCEPTION);
        CommonResp<Object> commonResp = new CommonResp<>();
        commonResp.setSuccess(false);
        commonResp.setMessage(BusniessExceptionEnum.CONFIRM_ORDER_FLOW_EXCEPTION.getDesc());
        return commonResp;
    }
}
